package com.yehangtech.designpattern.结构型_组合模式.service;

import java.util.ArrayList;
import java.util.List;

/**
 * 根对象和树枝对象都适合
 */
public class Composite extends Component {

    private String name;

    public Composite(String name) {
        this.name = name;
    }

    private final List<Component> childList = new ArrayList<>();

    @Override
    public String printSelf() {
        String res = name + "\n";
        for (Component component : childList) {
            res += component.printSelf();
        }
        res += "\n";
        return res;
    }

    @Override
    public void addChild(Component child) {
        this.childList.add(child);
    }
}
